﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.Service.TraceabilityService;
using MediatR;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.API.Application.Command.FarmCommand;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 养殖控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FarmController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<FarmController> logger;
        /// <summary>
        /// 养殖服务层
        /// </summary>
        IFarmService FarmS;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="FarmS"></param>
        /// <param name="mediator"></param>
        public FarmController(ILogger<FarmController> logger,
        IFarmService FarmS,
        IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
            this.FarmS = FarmS;
        }

        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="farm">要添加数据信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddFarm(AddFarmCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
               logger.LogError("【养殖控制器】添加养殖信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示养殖信息
        /// </summary>
        /// <param name="LOT">生产批次</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Farm>>> FarmInfo([FromQuery]FarmInfoCommand request)
        {
            ApiResult<List<Farm>> result = new ApiResult<List<Farm>>();
            try
            {
                result.Data=await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】显示养殖信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改养殖信息
        /// </summary>
        /// <param name="farm">修改后的养殖信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdFarm(UpdFarmCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】修改养殖信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="hygiene">要添加的数据信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddHygiene(AddHygieneCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】添加保健信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示保健信息列表
        /// </summary>
        /// <returns>返回数据信息集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<Hygiene>>> ListHygiene([FromQuery]ListHygieneCommand request)
        {
            ApiResult<List<Hygiene>> result = new ApiResult<List<Hygiene>>();
            try
            {
                result.Data = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】显示保健信息列表 出错"+ ex.Message);
                throw;
            }
            return result;
        }


        /// <summary>
        /// 反填保健信息
        /// </summary>
        /// <param name="Hygieneid">保健信息编号</param>
        /// <returns>返回一条保健信息</returns>
        [HttpGet]
        public async Task<ApiResult<Hygiene>> HygieneInfo(HygieneInfoCommand request)
        {
            ApiResult<Hygiene> result = new ApiResult<Hygiene>();
            try
            {
                result.Data = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】反填保健信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="Hygiene">修改后的保健信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdHygiene(UpdHygieneCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】修改保健信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="Hygieneid">要删除的保健信息编号</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelHygiene([FromQuery]DelHygieneCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖控制器】删除保健信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加企业部分信息
        /// </summary>
        /// <param name="farmFirm">养殖场</param>
        /// <param name="principal">负责人</param>
        /// <param name="NumberOfFields">存栏数量</param>
        /// <param name="LOT">批次</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddFirm([FromQuery]AddFirmCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖服务层】添加企业部分信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改企业部分信息
        /// </summary>
        /// <param name="farmFirm">养殖场</param>
        /// <param name="principal">负责人</param>
        /// <param name="NumberOfFields">存栏数量</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UPdirm([FromQuery]UPdirmCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖服务层】添加企业部分信息 出错"+ ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示企业信息
        /// </summary>
        /// <param name="LOT">批次</param>
        /// <returns>返回企业信息</returns>
        [HttpGet]
        public async Task<ApiResult<Firm>> ListFirm([FromQuery]ListFirmCommand request)
        {
            ApiResult<Firm> result = new ApiResult<Firm>();
            try
            {
                result.Data= await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【养殖服务层】修改企业部分信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

    }
}
